package Jets.loader
{
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	import Jets.interfaces.InJQueueLoader;
	
	public class JQueueLoader extends EventDispatcher implements InJQueueLoader
	{
		private var ms_loadCache:Dictionary = new Dictionary(true);
		private var ms_loadVector:Vector.<LoadInfo> = new Vector.<LoadInfo>();
		private var ms_loadCurrent:LoadInfo = null;
		private var ms_loader:JLoader = new JLoader();
		public function JQueueLoader(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function addRes(url:String, id:String, type:int):void
		{
			if(!ms_loadCache.hasOwnProperty(id))
			{
				ms_loadCache[id] = null;
				ms_loadVector.push(new LoadInfo(url,id,type));
			}
		}
		
		private function _load():void
		{
			if(ms_loadVector.length)
			{
				ms_loadCurrent = ms_loadVector.shift();
				ms_loader.onLoader(ms_loadCurrent.url,ms_loadCurrent.type,onLoadCompleted);
			}
			else
			{
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		private function onLoadCompleted(event:Event):void
		{
			_load();
		}
		
		public function startLoad():void
		{
			_load();
		}
		
		
		public function stopLoad():void
		{
		}
		
		public function setLoaderQueue(queue:Dictionary):void
		{
		}
	}
}

class LoadInfo
{
	public var url:String;
	public var id:String;
	public var type:int;
	public function LoadInfo(path:String,key:String,file:int)
	{
		url = path;	
		id = key;
		type = file;
	}
}